WarpConduit Computing

  • Quick Tips
  • Web Development
  • WordPress Plugins
  • Home
  • Passphrase Generator
  • Password Generator
  • About
  • Contact

Dynamically Load CSS and JS files using PHP

May 12, 2009 by Josh Hartman

I’ve been working more and more with jQuery and plugins that go with it and it can be a pain to keep going back to the code and adding another <script> tag for each of these and then removing it again when you find out it isn’t quite what you needed.  I came up with a solution to look in a “js” folder and create the correct tags for loading the files.  Then i extended this to do CSS files as well.  All you need to do is drop js/css files into “js” and “css” directories and the page you have placed the PHP code onto will load those files.  To disable a file, just delete it or move it into a subfolder. Oh, you are worried about loading order you say?  Since readdir() lists the files in alphabetical order you can rename your files using numbers (01-jquery.js, 02-jquery-plugin.js, 03-global.js, etc.) to adjust the loading order.  If you find any issues leave a comment to help others figure it out.  Hope you find it useful in some application.

Directory Structure

css

01-style.css
02-morestyles.css
03-custom.css

js

01-jquery.min.js
02-jquery.plugin.min.js
03-custom.js

index.php

Code

Place the following code just before your document’s closing </head> tag.

' . "\n";
        }
    }
    closedir($handle);
    echo $js;
}
 
// FOR CSS FILES
$css = '';
$handle = '';
$file = '';
// open the "css" directory
if ($handle = opendir('css')) {
    // list directory contents
    while (false !== ($file = readdir($handle))) {
        // only grab file names
        if (is_file('css/' . $file)) {
            // insert HTML code for loading Javascript files
            $css .= '' . "\n";
        }
    }
    closedir($handle);
    echo $css;
}
?>

Result





Dynamically Load CSS and JS files using PHP










Hello World!

Notes

This code will not search subdirectories for files to load.  This code also doesn’t help when you are using a WYSIWYG editor such as Adobe Dreamweaver that will look for JS/CSS links in the HTML header and load the files.  These programs don’t run the PHP code and therefore don’t know what JS/CSS files are suppose to be loaded. You may need to adjust the path to the “js” and “css” directories based on the location of the page you enter the code onto.

Filed Under: Web Development Tagged With: css, javascript, php

Comments

  1. ket says

    June 9, 2012 at 8:15 PM

    It’s not working.

    • Josh Hartman says

      June 15, 2012 at 8:32 PM

      Please provide more info and make sure the file paths fit your use. It works for me, besides a couple undefined index errors when all error reporting is on. I’ve updated the code to fix that.

  2. David S. says

    September 28, 2020 at 3:04 PM

    PHP is so useful for getting around limitations in the three Web languages! But I was surprised that you have almost duplicated code instead of writing a function and calling it twice. Also, why stop at creating multiple LINK and SCRIPT tags? Why not have PHP concatenate all the CSS files together, creating one file and one reference, and the same for JavaScript? And, for efficiency, the PHP code can even cache the concatenated files, rebuilding them whenever a component file has a later date-time-modified. That makes the PHP code larger, of course, but the result can be used in every website instead of using bloated tools that really only do the same thing.

Connect

  • Facebook
  • GitHub
  • RSS
  • Twitter
  • YouTube

Recent Posts

  • How to Permanently Remove the “Learn about this picture” Spotlight Wallpaper Icon From Your Windows Desktop
  • How to Quickly Test a Fax Machine
  • Extremely Useful Applications for Web Development and IT Tasks

Tags

automatic benchmark bigint class composer css embed escape event font function gzip helper htaccess html htmlspecialchars image increment javascript jquery list magento media mysql number observer opencart order output photo php profiling random redirect rijndael software text type ubuntu url windows windows 7 wordpress xampp xss

Blogroll

  • CodeIgniter
  • Fusion Forward
  • jQuery
  • Nettuts+
  • Smashing Magazine

© 2025 WarpConduit Computing. All Rights Reserved.